﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SelfManagement.Utility.Extensions;
using System.Web.Mvc;

namespace SelfManagement.Mvc.Result
{
    public class JsonResultData
    {
        public JsonResultData()
        {
            Messages = new List<string>();
        }

        public bool Success { get; set; }

        public string RedirectUrl { get; set; }

        public bool ReloadPage { get; set; }

        public object Model { get; set; }

        public int Code { get; set; }

        public List<string> Messages { get; set; }

        public void AddMessage(string message)
        {
            Messages.Add(message);
        }

        public void AddSuccessMessage(string message)
        {
            Success = true;
            Messages.Add(message);
        }

        public void AddModelValidErrorMessage(ModelStateDictionary dictionary)
        {
            Success = false;
            foreach (var kv in dictionary)
            {
                foreach (var err in kv.Value.Errors)
                {
                    AddMessage(err.ErrorMessage);
                }
            }
        }
    }
}
